<?php  
class Admin extends Controller{  
  
function Admin()  
{  
    parent::Controller();  
	$this->load->model('adminModel');
    $this->load->library('parser');  
    $this->load->helper('url');  
}  
  
	function index()
	{		
		if ($this->session->userdata('logged_in') == TRUE && $this->session->userdata('usertype') == 'admin')
		{	
			$data['message']="";
			echo "HAYPHOY";
			//$this->load->view('admin/template',$data);
		}
		else {
				redirect('admin/login/3');
				return false;
			}	
				
	}
function adminpage()
{
	$data['message']='Administrator Panel';
	$data['view']='adminwelcome';
	$data['page']=site_url('admin/pages/1');
	$this->load->view('admin',$data);

}	

function login()
{ 
	$logs = $this->uri->segment(3);
			$data['action'] = site_url('login/processlogin');
			if ($logs==1){
				$data['message'] = 'Please provide information both for username and password';
			}
			else if ($logs ==2){
				$data['message'] = 'Invalid Password. Please try again. Thank You.';
			}
			else if ($logs ==3){
				$data['message'] = 'You need to login to access this content. Thank You';
			}
			else if ($logs ==4){
				$data['message'] = 'Invalid Account. Please try again, thank you.';
			}
			else if ($logs ==5){
				$data['message'] = 'You just logged out';
			}
			else
			{
			$data['message'] = '';}
			$data['action'] = site_url('admin/processlogin');
			$this->load->view('homepage',$data);
}

function processlogin()
{
	$username = $this->input->post('username');
	$password = $this->input->post('password');
			if ($username =='' or $password == ''){
				//no input passed
				redirect ('admin/login/1');
			}
			//checks if the user is already logged in
			if($this->session->userdata('username')==$username){
			//	return false;
			}
			
			$this->db->where('username',$username);
			$query=$this->db->get('user');
			
			if ($query->num_rows() > 0){
				$row = $query->row();
				echo $row->password;
				if ($password != $row->password){
					redirect ('admin/login/4');
					return false;
				}
				//destroy old session if there exists
				$this->session->sess_destroy();
				//create a new session
				$this->session->sess_create();
				//remove the password field
				unset($row->password);
				//ser session data
				$this->session->set_userdata($row);
				//set logged in to true
				$this->session->set_userdata(array('logged_in' => true));
				//login was successful
				
				if ($row->usertype == 'admin')
				{
					redirect('admin/adminpage');
				}
				else
					echo "Your a Browser!";
					//redirect('admin/login');
				//if ($row->usertype == 'browser')
				//{
				//	echo "YOur a Browser!";
				//}
			}
			else
			{
				$data['message']="Invalid Account. Please try again.";
				$data['action'] = site_url('admin/processlogin');
				$this->load->view('homepage',$data);
			}
}
function logout()
{
	$this->session->sess_destroy();
	redirect('admin/login/5');
}
function announce()
{
	$data['message']='Administrator Panel';
	$data['query']= $this->adminModel->getallannounce();
	$data['view']='allannounce';
	$this->load->view('admin',$data);
}
function create_announce()
{
	$data['message']='Administrator Panel';
	$data['action']=site_url('admin/save_ann');
	$data['view']='create_announce';
	$this->load->view('admin',$data);
}
function save_ann()
{
	$this->form_validation->set_rules('title', 'Title', 'required');
	$this->form_validation->set_rules('announcement', 'Announcement', 'required');
	$this->form_validation->set_rules('status', 'Status', 'required');
	

			if ($this->form_validation->run() == FALSE)
				{	
					$data['message'] = 'Fill all textbox!';
					$data['action'] = site_url('admin/create_announce');
					$data['view'] = 'create_announce';
					$this->load->view('admin',$data);
				}
				else
				{
					
						$data = array(
							'title' => $_POST['title'],
							'announcement' => $_POST['announcement'],
							'status' => $_POST['status'],
							'author' => 'Admin',
							
						
						);
						$this->db->insert('announcement', $data);
						//$data['message'] = 'Account saved! Log now.';
						redirect('admin/announce');
					
				}
}
function editann($annid)
{
	$this->uri->segment(3);
	$data['action'] = site_url("admin/updateann/$annid");
	$data['view']='edit_ann';
	$data['message']='Administrator Panel';
	
	$this->db->where('annid',$annid);
					$query = $this->db->get('announcement');	
					$data['record'] = array(

						'title' => $query->row()->title,
						'announcement' => $query->row()->announcement,
						'status' => $query->row()->status,
						
					);
	$this->load->view('admin',$data);
}
function deleteann($annid)
{
	$this->uri->segment(3);
	$this->db->where('annid', $annid);
	$this->db->delete('announcement');
	redirect('admin/announce');
}
function updateann($annid)
{
	$this->uri->segment(3);
	$this->form_validation->set_rules('title', 'Title', 'required');
	$this->form_validation->set_rules('announcement', 'Announcement', 'required');
	$this->form_validation->set_rules('status', 'Status', 'required');
	
	if ($this->form_validation->run() == FALSE)
	{
		redirect("admin/editann/$annid");
	}else
	{
		$data = array(
						'title' => $_POST['title'],
						'announcement' => $_POST['announcement'],
						'status' => $_POST['status'],
					 );
					$this->db->where('annid', $annid);
					$this->db->update('announcement',$data);
					redirect('admin/announce');
	}
}




}